<?php
	function chong_pha_hoai()
	{
	}
	 
   function thongbao($c)
   {
   		?>
			<script type="text/javascript">
				alert("<?php echo $c; ?>");
			</script>
		<?php
   }
   
   function trangtruoc()
   {
   		?>
			<script type="text/javascript">
				window.history.back();
			</script>
		<?php
		exit();
   }
   
   function chuyentrang($link)
   {
   		?>
			<script type="text/javascript">
				window.location="<?php echo $link; ?>";
			</script>
		<?php
		exit();
   }
   
   function xac_dinh_menu_con_123($id_cha)
   {
   		$id=$_GET['id'];
      $tv="select count(*) from loaisp where(thuoc_id_loaisp='$id' and isActive=1)";
      //$tv_1=mysql_query($tv,$link)or die(mysql_error());
	  $tv_1=mysql_query($tv);
      $tv_2=mysql_fetch_row($tv_1);
      if($tv_2==0)
      {
         return "no";
      }
      else
      {
         return "yes";
      }
   }
   
   function de_quy_menu_fff($id_cha)
   {
   		$tv="select * from loaisp where (thuoc_id_loaisp='$id_cha')";
		$tv_1=mysql_query($tv);
		while($tv_2=mysql_fetch_array($tv_1))
		{
			$link="?thamso=xuat&id=$tv_2[id_loaisp]";
			echo"<li>";
				echo"<a href='$link'>";
					echo $tv_2['ten_loaisp'];
				echo"</a>";
				$xac_dinh_menu_con_123=xac_dinh_menu_con_123($tv_2['id_loaisp']);
				if($xac_dinh_menu_con_123=="yes")
				{
					echo "<ul>";
						de_quy_menu_fff($tv_2['id_loaisp']);
					echo "</ul>";
				}
			echo"</li>";
		}
	}
   
   //HÀM SHOW SẢN PHẨM TRONG MENU CON KHI CLICK VÀO MENU CHA
   function lay_chuoi_menu_con_uuu($id_cha,$c="")
   {
   		$tv="select * from loaisp where (thuoc_id_loaisp='$id_cha' and isActive=1)";
		$tv_1=mysql_query($tv);
		while($tv_2=mysql_fetch_array($tv_1))
		{
			$c=$c."_".$tv_2['id_loaisp'];
			$a_tv="select count(*) from loaisp where (thuoc_id_loaisp='$tv_2[id_loaisp]' and isActive=1)";
			$a_tv_1=mysql_query($a_tv);
			$a_tv_2=mysql_fetch_row($a_tv_1);
			if($a_tv_2[0]!=0)
			{
				$c=lay_chuoi_menu_con_uuu($tv_2['id_loaisp'],$c);
			}
		}
		return $c;
   }
   function lay_chuoi_menu_con_ppp($id_cha)
   {
   		$c=lay_chuoi_menu_con_uuu($id_cha);
		if($c=="")
		{
			return $id_cha;
		}
		else
		{
			return $id_cha.$c;
		}
   }
   function lay_mang_menu_con_ppp($id_cha)
   {
   		$c=lay_chuoi_menu_con_ppp($id_cha);
		$m=explode("_",$c);
		return $m;
		
   }
   function tinh_chuoi_union_ppp()
   {
   		$m=lay_mang_menu_con_ppp($_GET['id']);
		$tv="";
		for($i=0;$i<count($m);$i++)
		{
			$id=$m[$i];
			$r_tv="select count(*) from sanpham where (id_loaisp='$id' and isActive=1)";
			$r_tv_1=mysql_query($r_tv);
			$r_tv_2=mysql_fetch_row($r_tv_1);
			$so=$r_tv_2[0];
			$tv=$tv." ( select * from sanpham where (id_loaisp='$id' and isActive=1) limit 0,$so) union";
		}
		$tv=substr($tv,0,-6);
		return $tv;
   }
   //==========================================================================================================
   //TÍNH SỐ TRANG
   function tinh_so_trang($so_gioi_han)
   {
   		$m=lay_mang_menu_con_ppp($_GET['id']);
		$so=0;
		for($i=0;$i<count($m);$i++)
		{
			$id=$m[$i];
			$r_tv="select count(*) from sanpham where (id_loaisp='$id' and isActive=1)";
			$r_tv_1=mysql_query($r_tv);
			$r_tv_2=mysql_fetch_row($r_tv_1);
			$so=$so+$r_tv_2[0];
		}
		$st=ceil($so/$so_gioi_han);
		return $st;
   }
   //==========================================================================================================
   //SẢN PHẨM TRANG INDEX
   function xuat_ra_trang_index($id)
   {
		echo "<table border='1'>";
		$sql="select * from loaisp where id_loaisp=$id";
		$sql_1=mysql_query($sql);
		$sql_2=mysql_fetch_array($sql_1);
		$link="?thamso=xuat&id=$sql_2[id_loaisp]";
		echo "<a href='$link'>";
			echo "<p>$sql_2[ten_loaisp]</p>";
		echo "</a>";
		$ssp_td=4;
		$m=4;
		$tv="select * from sanpham where id_loaisp=$id ORDER BY time DESC";
		$tv_1=mysql_query($tv);
		$tv_2=mysql_fetch_array($tv_1);
			for($i=1;$i<=$ssp_td;$i++)
			{
				if($i!=1){$tv_2=mysql_fetch_array($tv_1);}
				if($i==1){echo "<tr>";}
				if($tv_2['ten_sanpham']!="")
				{
					$link_chi_tiet="?thamso=chi_tiet_san_pham&id=$tv_2[id_sanpham]";
					echo "<td align='center'>";
						$link_hinh="images/sanpham/".$tv_2['hinh'];
						echo "<a href='$link_chi_tiet'>";
							echo "<img border=0 src='$link_hinh' height='225' width='175'/>";
						echo "</a>";
						echo "<br>";
						echo "<a href='$link_chi_tiet'>";
							echo "<b>".$tv_2['ten_sanpham']."</b>";
						echo "</a>";
						echo "<br>";
						echo "Giá: ".$tv_2['dongia_sanpham']." VNĐ";
						echo "<br>";
						echo "Time: ".$tv_2['time'];
					echo "</td>";
				}	
				if($i==$ssp_td){echo "</tr>";}
			}//for
	echo "</table>";
   }
?>